<?php
/** DAO 模式用于把低级的数据访问 API 或操作从高级的业务服务中分离出来 */
interface DAO
{
    public function getAll();
    public function insert();
    public function update();
    public function remove();
}

/** 简单的POJO对象/Model */
class Student
{
    public $no;
    public $name;
    public function __construct($no, $name)
    {
        $this->no = $no;
        $this->name = $name;
    }
}

class StudentDAO
{
    /** @var Student[] $list */
    public $list = [];
    /** @return Student[] */
    public function getAll()
    {
        return $this->list;
    }
    /** @param Student $student */
    public function insert($student)
    {
        $this->list[] = $student;
    }
    /** @param Student $student */
    public function update($student)
    {
        foreach ($this->list as $key => $item) {
            if($item->no === $student->no){
                $this->list[$key] = $student;
                return true;
            }
        }
        return false;
    }
    /** @param Student $student */
    public function remove($student)
    {

        foreach ($this->list as $key => $item) {
            if($item->no === $student->no){
                unset($this->list[$key]);
            }
        }
    }
    public function __construct()
    {
        $this->insert(new Student("1","John"));
        $this->insert(new Student("2","Mary"));
    }
}

$dao = new StudentDAO;
print_r($dao->getAll());
